package com.system.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.common.bean.User;
import com.system.common.exception.ServiceException;
import com.system.common.service.IUserService;
import com.system.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    public User getUserById(Integer id) {
        return super.getById(id);
    }

    @Override
    public User getUserByUsername(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        return super.getOne(queryWrapper);
    }

    @Override
    public void addUser(User user) {
        super.save(user);
    }

    @Override
    public void updateUser(User user) {
        if (user == null) {
            throw new ServiceException("用户不存在");
        }
        if(user.getIsdel()==1){
            throw new ServiceException("用户已删除");
        }
        super.updateById(user);
    }

    @Override
    public void deleteUser(Integer id) {
        User user = super.getById(id);
        if (user == null) {
            throw new ServiceException("用户不存在");
        }
        if(user.getIsdel()==1){
            throw new ServiceException("用户已删除");
        }
        super.removeById(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        User user = super.getById(id);
        if (user == null) {
            throw new ServiceException("用户不存在");
        }
        user.setStatus(status);
        super.updateById(user);
    }

    @Override
    public List<User> getUserList() {
        return super.list();
    }

    @Override
    public Page<User> getPage(User user, Page<User> page) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
    
        // 添加查询条件
        if (user != null) {
            // 用户名模糊查询
            if (user.getUsername() != null && !user.getUsername().isEmpty()) {
                queryWrapper.like(User::getUsername, user.getUsername());
            }
    
            // 状态精确查询
            if (user.getStatus() != null) {
                queryWrapper.eq(User::getStatus, user.getStatus());
            }
    
            // 手机号模糊查询
            if (user.getPhone() != null && !user.getPhone().isEmpty()) {
                queryWrapper.like(User::getPhone, user.getPhone());
            }
            if (user.getGender() != null) {
                queryWrapper.eq(User::getGender, user.getGender());
            }
        }
    
        // 执行分页查询 - MyBatis-Plus会自动处理分页
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public User getUserWithAddress(Integer userid) {
        return baseMapper.selectUserWithAddress(userid);
    }

    @Override
    public List<User> getUserWithAddressList() {
        return baseMapper.selectUserWithAddressList();
    }
}
